package com.huyaanalysisplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huyaanalysisplatform.common.Result;
import com.huyaanalysisplatform.entity.TAreaHot;
import com.huyaanalysisplatform.entity.TUser;
import com.huyaanalysisplatform.mapper.TAreaHotMapper;
import com.huyaanalysisplatform.service.TAreaHotService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jxl.write.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Huadi Group 12
 * @since 2021-08-01
 */
@Service
public class TAreaHotServiceImpl extends ServiceImpl<TAreaHotMapper, TAreaHot> implements TAreaHotService {

    @Autowired
    TAreaHotMapper tAreaHotMapper;

    @Override
    public Map<String,List<Map<String, Object>>> hotShowEcharts() {
        List<TAreaHot> tAreaHots = tAreaHotMapper.selectOneDay();
        List<Map<String,Object>> list = new ArrayList<>();
        List<TAreaHot> tempList = new ArrayList<>();
        for(TAreaHot tAreaHot : tAreaHots){
            tempList.add(tAreaHot);
            if (tempList.size()==4){
                Map<String, Object> tAreaHotMap = new HashMap<>();
                //tAreaHotMap.put("time",  (new Date().getTime() - tempList.get(0).getDate().getTime())/(1000 * 60 * 60));
                tAreaHotMap.put("time",24 - tempList.get(0).getDate().getHour());
                for(TAreaHot tempTAreaHot : tempList){
                    if(tempTAreaHot.getAreaName().equals("网游竞技")){
                        tAreaHotMap.put("netGame", tempTAreaHot.getTotalHot());
                    }
                    if (tempTAreaHot.getAreaName().equals("单机热游")){
                        tAreaHotMap.put("singleGame", tempTAreaHot.getTotalHot());
                    }
                    if(tempTAreaHot.getAreaName().equals("手游休闲")){
                        tAreaHotMap.put("phoneGame",tempTAreaHot.getTotalHot());
                    }
                    if(tempTAreaHot.getAreaName().equals("娱乐天地")){
                        tAreaHotMap.put("funGame",tempTAreaHot.getTotalHot());
                    }
                }
                list.add(tAreaHotMap);
                tempList.clear();
            }
        }
        Map<String, List<Map<String,Object>>> map = new HashMap<>();
        map.put("areaHot", list);
        return map;
    }

    @Override
    public Map<String, List<TAreaHot>> hotShowTable() {
        List<TAreaHot> tAreaHots = tAreaHotMapper.selectAll();
        Map<String,List<TAreaHot>> map = new HashMap<>();
        map.put("tableData", tAreaHots);
        return map;
    }

    @Override
    public Map<String, List<Map<String, Object>>> totalHotShow() {

        List<TAreaHot> tAreaHots = tAreaHotMapper.selectOneDay();

        List<Map<String,Object>> list = new ArrayList<>();
        List<TAreaHot> tempList = new ArrayList<>();
        for(TAreaHot tAreaHot : tAreaHots){
            tempList.add(tAreaHot);
            if (tempList.size()==4){
                Map<String, Object> tAreaHotMap = new HashMap<>();
                long totalHotOfFourAreas = 0;
               // tAreaHotMap.put("time", (new Date().getTime() - tempList.get(0).getDate().getTime())/(1000 * 60 * 60));
                tAreaHotMap.put("time",24 - tempList.get(0).getDate().getHour());
                for(TAreaHot tempTAreaHot : tempList) {
                    totalHotOfFourAreas = totalHotOfFourAreas + tempTAreaHot.getTotalHot();
                }
                tAreaHotMap.put("count",totalHotOfFourAreas);
                list.add(tAreaHotMap);
                tempList.clear();
            }
        }
        Map<String, List<Map<String,Object>>> map = new HashMap<>();
        map.put("totalHot", list);
        return map;
    }

    @Override
    public Result add(TAreaHot tAreaHot) {
        if (tAreaHot.getTotalHot() < 0){
            return Result.error().code(3001);
        }
        tAreaHot.setId(UUID.randomUUID().toString().replace("-",""));
        this.save(tAreaHot);
        return Result.success().code(2001);
    }

    @Override
    public Result edit(TAreaHot tAreaHot) {
        if (tAreaHot.getTotalHot() < 0){
            return Result.error().code(3001);
        }
        this.updateById(tAreaHot);
        return Result.success().code(2001);
    }

    @Override
    public Result delete(String id) {
        tAreaHotMapper.deleteById(id);
        return Result.success().code(2001);
    }

    @Override
    public Map<String, List<Map<String, Object>>> totalHotOfNetGame() {
        List<TAreaHot> tAreaHots = tAreaHotMapper.selectNetGame1();
        List<TAreaHot> tAreaHotList = tAreaHotMapper.selectNetGame2();
        List<Map<String,Object>> list = new ArrayList<>();
        for (int i = 0; i < tAreaHots.size(); ++i){
            Map<String, Object> tAreaHotMap = new HashMap<>();
            tAreaHotMap.put("time",24 - tAreaHots.get(i).getDate().getHour());
            tAreaHotMap.put("count",(tAreaHots.get(i).getTotalHot()+tAreaHotList.get(i).getTotalHot())/2);
            list.add(tAreaHotMap);
        }
        Map<String, List<Map<String, Object>>> map = new HashMap<>();
        map.put("totalHot",list);
        return map;
    }

    @Override
    public Map<String, List<Map<String, Object>>> totalHotOfSingleGame() {
        List<TAreaHot> tAreaHots = tAreaHotMapper.selectSingleGame1();
        List<TAreaHot> tAreaHotList = tAreaHotMapper.selectSingleGame2();
        List<Map<String,Object>> list = new ArrayList<>();
        for (int i = 0; i < tAreaHots.size(); ++i){
            Map<String, Object> tAreaHotMap = new HashMap<>();
            tAreaHotMap.put("time",24 - tAreaHots.get(i).getDate().getHour());
            tAreaHotMap.put("count",(tAreaHots.get(i).getTotalHot()+tAreaHotList.get(i).getTotalHot())/2);
            list.add(tAreaHotMap);
        }
        Map<String, List<Map<String, Object>>> map = new HashMap<>();
        map.put("totalHot",list);
        return map;
    }

    @Override
    public Map<String, List<Map<String, Object>>> totalHotOfFunGame() {
        List<TAreaHot> tAreaHots = tAreaHotMapper.selectFunGame1();
        List<TAreaHot> tAreaHotList = tAreaHotMapper.selectFunGame2();
        List<Map<String,Object>> list = new ArrayList<>();
        for (int i = 0; i < tAreaHots.size(); ++i){
            Map<String, Object> tAreaHotMap = new HashMap<>();
            tAreaHotMap.put("time",24 - tAreaHots.get(i).getDate().getHour());
            tAreaHotMap.put("count",(tAreaHots.get(i).getTotalHot()+tAreaHotList.get(i).getTotalHot())/2);
            list.add(tAreaHotMap);
        }
        Map<String, List<Map<String, Object>>> map = new HashMap<>();
        map.put("totalHot",list);
        return map;
    }

    @Override
    public Map<String, List<Map<String, Object>>> totalHotOfPhoneGame() {
        List<TAreaHot> tAreaHots = tAreaHotMapper.selectPhoneGame1();
        List<TAreaHot> tAreaHotList = tAreaHotMapper.selectPhoneGame2();
        List<Map<String,Object>> list = new ArrayList<>();
        for (int i = 0; i < tAreaHots.size(); ++i){
            Map<String, Object> tAreaHotMap = new HashMap<>();
            tAreaHotMap.put("time",24 - tAreaHots.get(i).getDate().getHour());
            tAreaHotMap.put("count",(tAreaHots.get(i).getTotalHot()+tAreaHotList.get(i).getTotalHot())/2);
            list.add(tAreaHotMap);
        }
        Map<String, List<Map<String, Object>>> map = new HashMap<>();
        map.put("totalHot",list);
        return map;
    }


}
